package com.module.launcher.model

import android.text.TextUtils
import com.module.app.base.BaseViewModel
import com.module.base.utils.ForegroundCallbacks
import com.module.frame.ext.postEvent
import com.module.launcher.utils.checkServiceUtils
import kotlinx.coroutines.flow.MutableSharedFlow

class MainViewModel : BaseViewModel() {
    init {
        checkServiceUtils.check()
        ForegroundCallbacks.get(com.module.app.base.BaseApplication.getContext())
            .addListener(object : ForegroundCallbacks.Listener {
                override fun onBecameForeground() {
                    checkServiceUtils.check()
                }

                override fun onBecameBackground() {
                    checkServiceUtils.destroy()
                }
            })
    }
    /**
     * 公告flow
     */
    var noticeFlow: MutableSharedFlow<String> = MutableSharedFlow()

    /**
     * 更新flow
     */
    var updateFlow: MutableSharedFlow<Boolean> = MutableSharedFlow()
    fun getConfig() {
        //由于在欢迎界面请求过了，这边就不在继续请求了，直接拿数据
        if (com.module.app.bean.ConfigBean.getInstance().isUpdate) {
            postEvent(updateFlow,true)
        } else if (!TextUtils.isEmpty(com.module.app.bean.ConfigBean.getInstance().notice)) {
            postEvent(noticeFlow, com.module.app.bean.ConfigBean.getInstance().notice)
        }
    }
}